package com.herohead;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import com.healthmarketscience.jackcess.Cursor;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;

public class ServerDB {

	public static Boolean isNotNull(String value) {

		if (value == null) return false;
		else return true;

	}
	public static String lookupAccessFile(String DBFile,String lookupvalue) {

		String col = "Server_ServerName";
		String strItOwner = null;
		String strOwnerID = null;
		String strFrrs = null;




		//System.out.println(misc.callex("ping us01s-netops01"));

		ServerDB lookup = new ServerDB();

		//lookup.setDBFile("D:\\personal\\sugar_work\\CSIDB.accdb");
		lookup.setDBFile(DBFile);
		//System.out.println(sdb.getDBFile());
		//sdb.setDBFile("/Users/herohead/Documents/sugar_work/CSIDB.accdb");
		try {
			lookup.openDatabase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			lookup.openTable("tabAllInformations");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String app;
		try {
			app = lookup.getDBValue(col, lookupvalue,"Application");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			app = null;
		}
		String frrs;
		try {
			frrs = lookup.getDBValue(col, lookupvalue,"frrs_id");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			frrs = null;
		}
		String it_owner;
		try {
			it_owner = lookup.getDBValue(col, lookupvalue,"it_responsible_id");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			it_owner = null;
		}
		String owner_id;
		try {
			owner_id = lookup.getDBValue(col, lookupvalue,"system_owner_id");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			owner_id = null;
		}

		try {
			lookup.openTable("tbl_ddv");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (isNotNull(it_owner)) {
			int y = Integer.parseInt(it_owner);
			try {
				strItOwner = lookup.getDBInt("ddv_id", y,"ddv_value");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				strItOwner = null;
			}
		}

		if (isNotNull(owner_id)) {
			int y = Integer.parseInt(owner_id);
			try {
				strOwnerID= lookup.getDBInt("ddv_id", y,"ddv_value");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				strOwnerID = null;
			}
		}

		if (isNotNull(frrs)) {
			int y = Integer.parseInt(frrs);
			try {
				strFrrs = lookup.getDBInt("ddv_id", y,"ddv_value");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				strFrrs =  null;
			}
		}

		try {
			lookup.closeDB();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return app + "\n" + strItOwner + "\n" + strOwnerID + "\n" + strFrrs;

	}
	//private static String mainTable = "tabAllInformations";
	private String dbFile = null;

	public Database db;

	private Table tab;

	public void closeDB() throws IOException {
		db.close();
	}

	public String getDBFile() {
		return dbFile;
	}

	public String getDBInt(String col,int serverLU, String returnCol ) throws IOException {

		Map<String, Object> row = Cursor.findRow(tab, Collections.singletonMap(col, serverLU));
		if(row != null) {
			if (row.get(returnCol) != null) {
				return row.get(returnCol).toString();
			}
			// System.out.println(system_owner);
			return null;
		} else {
			return "Value " + serverLU + " not found in col " + col;
		}
	}

	public String getDBValue(String col, String serverLU, String returnCol ) throws IOException {

		Map<String, Object> row = Cursor.findRow(tab, Collections.singletonMap(col, serverLU));
		if(row != null) {
			if (row.get(returnCol) != null) {
				return row.get(returnCol).toString();
			}
			// System.out.println(system_owner);
			return null;
		} else {
			return "Value " + serverLU + " not found in col " + col;
		}
	}




	public void openDatabase() throws IOException {

		db = Database.open(new File(dbFile));
	}

	public void openTable(String mainTable) throws IOException {

		tab = db.getTable(mainTable);
	}



	public void setDBFile(String strDbFile) {
		dbFile = strDbFile;
	}



}
